﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using StockholmsStad.Windows.UI.Facade.Events;
using StockholmsStad.Windows.UI.Facade.ParvisService;
using StockholmsStad.Windows.UI.Facade.MaintenanceService;
using StockholmsStad.Windows.UI.Facade.CeremonyService;

namespace StockholmsStad.Windows.UI.Facade
{
    public class MaintenanceFacade : FacadeBase
    {
        public event EventHandler<CeremonySummaryEventArgs> GetOrdinaryCeremoniesCompleted;

        public MaintenanceFacade(User user)
            : base(user)
        {
        }

        public List<MinisterTimeBlock> GetTimeBlocks()
        {            
            var client = new WinServiceClient();
            try
            {
                var list = new List<MinisterTimeBlock>();
                var timeBlocks = client.GetTimeBlocks(CurrentUser);
                client.Close();
                list.AddRange(timeBlocks);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public ObservableCollection<MinisterTimeBlock> GetTimeBlocks(DateTime date)
        {
            var client = new WinServiceClient();
            try
            {
                var newTimeBlocks = new ObservableCollection<MinisterTimeBlock>();
                var timeBlocks = client.GetTimeBlocksForDate(CurrentUser, date);
                client.Close();

                foreach (var t in timeBlocks)
                {
                    newTimeBlocks.Add(t);
                }

                return newTimeBlocks;

            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<MinisterTimeBlock> UpdateTimeBlocks(List<MinisterTimeBlock> timeblocks)
        {
            var client = new WinServiceClient();
            try
            {
                var blocks = new List<MinisterTimeBlock>();
                var updatedBlocks = client.UpdateTimeBlocks(CurrentUser, timeblocks.ToArray());
                client.Close();
                blocks.AddRange(updatedBlocks);
                return blocks;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public int AddText(InformationText text)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var textId = client.AddText(CurrentUser, text);
                client.Close();
                return textId;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void UpdateText(InformationText text)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.UpdateText(CurrentUser, text);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void DeleteText(InformationText text)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.DeleteText(CurrentUser, text);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public InformationText GetText(int textId)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var text = client.GetText(CurrentUser, textId);
                client.Close();
                return text;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<InformationText> GetTexts(int textTypeId)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var list = new List<InformationText>();
                var texts = client.GetTexts(CurrentUser, textTypeId);
                client.Close();
                list.AddRange(texts);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public int AddSpecialNeed(string name)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var id = client.AddSpecialNeed(CurrentUser, name);
                client.Close();
                return id;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public int AddLanguage(string languageName)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var id = client.AddLanguage(CurrentUser, languageName);
                client.Close();
                return id;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public int AddCeremonyLanguage(string languageName)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var id = client.AddCeremonyLanguage(CurrentUser, languageName);
                client.Close();
                return id;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public int AddCertificateLanguage(string languageName)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var id = client.AddCertificateLanguage(CurrentUser, languageName);
                client.Close();
                return id;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<ValueIdPair> GetLanguages()
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var list = new List<ValueIdPair>();
                var texts = client.GetLanguages(CurrentUser);
                client.Close();
                list.AddRange(texts);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }            
        }

        public List<ValueIdPair> GetAvailableLanguagesForReportDefinition(MarriageCertificateType marriageCertificateType)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var list = new List<ValueIdPair>();
                var texts = client.GetAvailableLanguagesForReportDefinition(CurrentUser, marriageCertificateType);
                client.Close();
                list.AddRange(texts);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }            
        }

        /// <summary>
        /// Hämtar all texttyper
        /// </summary>
        /// <returns>En lista med texttyper (<see cref="ValueIdPair"/>).</returns>
        public List<ValueIdPair> GetTextTypes()
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var list = new List<ValueIdPair>();
                var texts = client.GetTextTypes(CurrentUser);
                client.Close();
                list.AddRange(texts);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<ValueIdPair> GetLanguagesForCertificateType(MarriageCertificateType certificateType)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var list = new List<ValueIdPair>();
                var texts = client.GetLanguagesForCertificateType(CurrentUser, certificateType);
                client.Close();
                list.AddRange(texts);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public List<MarriageCertificateType> GetMarriageCertificateTypes()
        {
            var list = Enum.GetValues(typeof(MarriageCertificateType));

            var types = new List<MarriageCertificateType>();
            foreach (var item in list)
            {
                var eItem = (MarriageCertificateType) item;
                types.Add(eItem);
            }

            return types;
        }

        public int AddMarriageCertificate(MarriageCertificate certificate)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var id = client.AddMarriageCertificate(CurrentUser, certificate);
                client.Close();
                return id;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }

        }

        /// <summary>
        /// Uppdaterar en befintlig rapportdefinition. 
        /// </summary>
        /// <param name="certificate">Innehållet på rapportdefinitionen. </param>
        /// <returns> </returns>
        public void UpdateMarriageCertificate(MarriageCertificate certificate)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.UpdateMarriageCertificate(CurrentUser, certificate);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }

        }

        /// <summary>
        /// Tar bort en befintlig rapportdefinition. 
        /// </summary>
        /// <param name="certificate">Innehållet på rapportdefinitionen. </param>
        /// <returns> </returns>
        public void DeleteMarriageCertificate(MarriageCertificate certificate)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.DeleteMarriageCertificate(CurrentUser, certificate);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }

        }

        /// <summary>
        /// Hämtar ett registreringsbevis. 
        /// </summary>
        /// <param name="marriageCertificateType">Id-numret på det aktuella beviset. </param>
        /// <param name="languageId">Id-numret på det aktuella beviset. </param>
        /// <returns>Registreringsbeviset. </returns>
        public MarriageCertificate GetMarriageCertificate(MarriageCertificateType marriageCertificateType, int languageId)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var cert = client.GetMarriageCertificate(CurrentUser, marriageCertificateType, languageId);
                client.Close();
                return cert;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }


        public ParVisProperties GetProperties()
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var properties = client.GetProperties(CurrentUser);
                client.Close();
                return properties;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void UpdateProperties(ParVisProperties properties)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.SetProperties(CurrentUser, properties);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        //public void GetOrdinaryCeremoniesASync(DateTime date)
        //{
        //    var client = new MaintenanceServiceClient();

        //    try
        //    {
        //        client.GetOrdinaryCeremonyBookingForDateCompleted += client_GetOrdinaryCeremonyBookingForDateCompleted;
        //        client.GetOrdinaryCeremonyBookingForDateAsync(CurrentUser, date);
        //    }
        //    catch (Exception)
        //    {
        //        client.Abort();
        //        throw;
        //    }
        //}

        //public void GetOrdinaryCeremoniesASync(DateTime fromDate, DateTime? toDate, int? ministerId)
        //{
        //    var client = new MaintenanceServiceClient();

        //    try
        //    {
        //        client.GetOrdinaryCeremonyBookingCompleted += client_GetOrdinaryCeremonyBookingCompleted;
        //        client.GetOrdinaryCeremonyBookingAsync(CurrentUser, fromDate, toDate, ministerId);
        //    }
        //    catch (Exception)
        //    {
        //        client.Abort();
        //        throw;
        //    }
        //}

        public List<Couple> GetCouplesForDepersonation(DateTime fromDate, DateTime toDate)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var list = new List<Couple>();
                var couples = client.GetCouplesForDepersonation(CurrentUser, fromDate, toDate);
                client.Close();
                list.AddRange(couples);
                return list;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public int DepersonateCouples(List<Couple> couples)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                var retVal = client.DepersonateCouples(CurrentUser, couples.ToArray());
                client.Close();
                return retVal;
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void DeleteCeremonyLanguage(ValueIdPair language)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.DeleteCeremonyLanguage(CurrentUser, language);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void DeleteCertificateLanguage(ValueIdPair language)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.DeleteCertificateLanguage(CurrentUser, language);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void DeleteLanguage(ValueIdPair language)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.DeleteLanguage(CurrentUser, language);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        public void UpdateLanguage(ValueIdPair language)
        {
            var client = new MaintenanceServiceClient();
            try
            {
                client.UpdateLanguage(CurrentUser, language);
                client.Close();
            }
            catch (Exception)
            {
                client.Abort();
                throw;
            }
        }

        //private void client_GetOrdinaryCeremonyBookingCompleted(object sender, GetOrdinaryCeremonyBookingCompletedEventArgs e)
        //{
        //    if (e.Error != null)
        //        throw e.Error;

        //    var eventArgs = new CeremonySummaryEventArgs { Data = new List<CeremonySummary>(e.Result) };

        //    if (GetOrdinaryCeremoniesCompleted != null)
        //        GetOrdinaryCeremoniesCompleted(this, eventArgs);
        //}


        //private void client_GetOrdinaryCeremonyBookingForDateCompleted(object sender, GetOrdinaryCeremonyBookingForDateCompletedEventArgs e)
        //{
        //    if (e.Error != null)
        //        throw e.Error;

        //    var eventArgs = new CeremonySummaryEventArgs { Data = new List<CeremonySummary>(e.Result) };

        //    if (GetOrdinaryCeremoniesCompleted != null)
        //        GetOrdinaryCeremoniesCompleted(this, eventArgs);
        //}
    }
}
